<template>
  <div>
    <h1>用户押金记录</h1>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-input
                  v-model="form.realname"
                  style="width: 100%"
                  placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关键码">
              <el-input
                  v-model="form.phone"
                  style="width: 100%"
                  placeholder="手机号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <el-date-picker
                  v-model="createTimes"
                  type="datetimerange"
                  range-separator="To"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  format="YYYY-MM-DD HH:mm:ss"
                  value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>
    <!---数据表格-->
    <el-table :data="depositUser"  style="width: 100%">
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="sex" label="性别" #default="scope" >
        {{scope.row.sex == 0 ? '男' : '女'}}
      </el-table-column>
      <el-table-column prop="phone" label="手机号" #default="scope">
        {{scope.row.phone.substring(0,3)}}******{{scope.row.phone.substring(7,11)}}
      </el-table-column>
      <el-table-column prop="cardid" label="身份证" #default="scope">
        {{scope.row.cardid.substring(0,6)}}******{{scope.row.cardid.substring(14,18)}}
      </el-table-column>
      <el-table-column prop="depositUserTime" label="退还时间" />
      <el-table-column prop="amount" label="退还押金" />
      <el-table-column prop="r" label="退款情况" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="danger" size="small" @click="deleteById(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>
    <!--分页条-->
    <el-pagination
        v-model:current-page="pageParamsList.page"
        v-model:page-size="pageParamsList.limit"
        :page-sizes="[2, 20, 50, 100]"
        @size-change="fetchSysUser"
        @current-change="fetchSysUser"
        layout="total, sizes, prev, pager, next"
        :total="total"
    />
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {getDeleteUser, getUserList} from "@/api/changebattery/userManager";

const queryDto = {
  realname: '',
  createTimeBegin: '',
  createTimeEnd: '',
  phone:'',
  state:0
}


//开始和结束时间数据模型
const createTimes = ref([])

const form = ref(queryDto)

const depositUser =ref([])

//重置
const resetData = async () => {
  form.value.realname = ''
  form.value.phone = ''
  form.value.createTimeBegin = ''
  form.value.createTimeEnd = ''
  createTimes.value=[]
  await fetchSysUser()
}

// 删除数据
const deleteById = row => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getDeleteUser(row.id)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParamsUsers.value.page = 1
      await fetchSysUser()
    }
  })
}

//搜索方法
const searchSysUser = () => {
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}


//列表
const fetchSysUser = async () => {
  // listUser.value=true
  // show.value=false
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getUserList(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
  depositUser.value = data.depositUser.records
  total.value = data.depositUser.total
}
onMounted(fetchSysUser)
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)

</script>

<style scoped>

</style>
